package com.yanfan.accessControl.quartz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanfan.accessControl.domain.IbmsAccessControlRecords;
import com.yanfan.accessControl.domain.IbmsAccessFlow;
import com.yanfan.accessControl.mapper.IbmsAccessControlRecordsMapper;
import com.yanfan.accessControl.mapper.IbmsAccessFlowMapper;
import com.yanfan.accessControl.service.IIbmsAccessControlRecordsService;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.collection.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component("accessHourTask")
public class AccessHourTask {
    @Autowired
    private IIbmsAccessControlRecordsService ibmsAccessControlRecordsService;

    @Autowired
    private IbmsAccessControlRecordsMapper ibmsAccessControlRecordsMapper;

    @Autowired
    private IbmsAccessFlowMapper ibmsAccessFlowMapper;


    public void accessFlowHour() {
        // 获取当前时间往前推一小时的时间
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        Date oneHourAgo = calendar.getTime();
        String now = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", nowDate);
        String passHour = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", oneHourAgo);

        List<IbmsAccessFlow> ibmsAccessFlows = ibmsAccessControlRecordsMapper.accessFlow(passHour, now);
        ArrayList<IbmsAccessFlow> flows = new ArrayList<>();
        if (!CollectionUtils.isAnyEmpty(ibmsAccessFlows)){
            for (IbmsAccessFlow ibmsAccessFlow : ibmsAccessFlows) {
                ibmsAccessFlow.setCountTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:00:00", nowDate));
                flows.add(ibmsAccessFlow);
                ibmsAccessFlowMapper.insertBatch(flows);
            }
        }else {
            IbmsAccessFlow ibmsAccessFlow = new IbmsAccessFlow();
            ibmsAccessFlow.setFlow(0L);
            ibmsAccessFlow.setInOutType("进开门");
            ibmsAccessFlow.setCountTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:00:00", nowDate));
            ibmsAccessFlowMapper.insert(ibmsAccessFlow);
        }

    }


}
